﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows;

namespace QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc
{
    class DONTHUOC_CT_Control : IDisposable
    {
        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);

        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
        }
        public void addData(ListBox list1, string ma_dt)
        {
            foreach (var i in list1.Items)
            {
                var temp = (DUOCPHAM_Get_ListResult)i;
                db.DONTHUOC_CT_Insert(temp.MA_DUOCPHAM, ma_dt);
            }
        }
        public void updateData(ListBox list1, string ma_dt)
        {
            deleteData(ma_dt);
            addData(list1, ma_dt);
        }
        public void deleteData(string ma_dt)
        {
            foreach (var t in db.DONTHUOC_CT_GetByMaDonThuoc(ma_dt))
            {
                db.DONTHUOC_CT_Delete(t.MA_DUOCPHAM, ma_dt);
            }
        }
        public void loadListBox_Edit( string ID_Edit, ListBox list1)
        {
            var ls = db.DONTHUOC_CT_GetByMaDonThuoc(ID_Edit);

            foreach (var i in ls)
            {
                var temp = db.DUOCPHAM_GetByMaDuocpham(i.MA_DUOCPHAM);
                var typeorder = temp.OrderBy(w => w.TEN_DUOCPHAM);
                foreach (var j in typeorder)
                {
                    list1.Items.Add(j);
                }
                
            }

        }
    }
}
